using System;
using System.Xml;
using System.Xml.XPath;

namespace umbracoRss.BusinessLogic
{
	/// <summary>
	/// Summary description for Data.
	/// </summary>
	internal class Data
	{
		private static string _sourcePath = System.Web.HttpContext.Current.Server.MapPath(umbraco.GlobalSettings.StorageDirectory + "/rss/umbracoRssInstances.xml");
		private static XmlDocument _source;

		public static XmlDocument Source 
		{
			get {
				if (_source == null) 
					Reload();

				return _source;
			}
		}

		public static void Reload() 
		{
			if (_source == null)
				_source = new XmlDocument();
			_source.Load(_sourcePath);
		}

		public static XmlNode GetFromId(int Id) 
		{
			return Source.SelectSingleNode("/umbracoRss/instances/instance [@id = '" + Id.ToString().ToUpper() + "']");
		}

		public static void MakeNew(string Title) 
		{
			int _maxId = 1;
			// Find max id
			foreach (XmlNode n in Source.SelectNodes("umbracoRss/instances/instance")) 
			{
				if (int.Parse(n.Attributes.GetNamedItem("id").Value) >= _maxId)
					_maxId = int.Parse(n.Attributes.GetNamedItem("id").Value)+1;
			}

			XmlElement instance = Source.CreateElement("instance");
			instance.Attributes.Append(umbraco.xmlHelper.addAttribute(Source, "id", _maxId.ToString()));
			instance.AppendChild(umbraco.xmlHelper.addTextNode(Source, "title", Title));
			instance.AppendChild(umbraco.xmlHelper.addTextNode(Source, "ttl", "120"));
			instance.AppendChild(umbraco.xmlHelper.addTextNode(Source, "copyright", ""));
			instance.AppendChild(umbraco.xmlHelper.addTextNode(Source, "category", ""));
			instance.AppendChild(umbraco.xmlHelper.addTextNode(Source, "url", Title.Replace(" ", "") + ".xml"));
			instance.AppendChild(umbraco.xmlHelper.addTextNode(Source, "documentId", "0"));
			instance.AppendChild(umbraco.xmlHelper.addTextNode(Source, "fullPath", "false"));
			instance.AppendChild(umbraco.xmlHelper.addTextNode(Source, "descriptionSourceAlias", ""));
			instance.AppendChild(umbraco.xmlHelper.addTextNode(Source, "documentTypeFilterAlias", ""));
			instance.AppendChild(umbraco.xmlHelper.addTextNode(Source, "propertyTypeFilterAlias", ""));
			instance.AppendChild(umbraco.xmlHelper.addTextNode(Source, "filterValue", ""));
			instance.AppendChild(umbraco.xmlHelper.addTextNode(Source, "author", ""));
			instance.AppendChild(umbraco.xmlHelper.addTextNode(Source, "pubdatefield", ""));
			Source.SelectSingleNode("umbracoRss/instances").AppendChild(instance);
			Save();
		}

		public static void Delete(int Id) 
		{
			// Remove physical rss file if any
			RssInstance i = new RssInstance(Id);

			// We might not be able to delete file because of permission locking
			// we ought to check for that, but this is the dirty solution - sorry, but that!
			try 
			{
				if (System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(umbraco.GlobalSettings.StorageDirectory + "/rss/" + i.Url)))
					System.IO.File.Delete(System.Web.HttpContext.Current.Server.MapPath(umbraco.GlobalSettings.StorageDirectory + "/rss/" + i.Url));
			} 
			catch {}

			XmlNode n = Data.GetFromId(Id);
			Data.Source.SelectSingleNode("/umbracoRss/instances").RemoveChild(n);
			Data.Save();
		}

		public static void UpdateValue(XmlNode n, string Value) 
		{
			if (n.FirstChild != null)
				n.FirstChild.Value = Value;
			else 
			{
				n.AppendChild(Source.CreateTextNode(Value));
			}
			Save();
		}

		public static void Save() 
		{
			if (System.IO.File.Exists(_sourcePath))
			{
				System.IO.FileStream f = System.IO.File.Open(_sourcePath, System.IO.FileMode.Create);
				System.IO.StreamWriter sw = new System.IO.StreamWriter(f);
				sw.WriteLine(_source.OuterXml);
				sw.Close();
				f.Close();
			}
			
		}

		public Data()
		{

		}
	}
}
